package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.DTO.DataOverviewVO;
import com.ruoyi.common.core.domain.DTO.DataPerformanceBO;
import com.ruoyi.common.core.domain.DTO.DataPerformanceVO;
import com.ruoyi.common.core.domain.DTO.PerformanceVO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.PurchaseRecordMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.WithdrawalRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Transactional
public class DataDisplayService {

    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private WithdrawalRecordMapper withdrawalRecordMapper;

    /**
     * 业绩列表
     *
     * @param dataPerformanceBO
     * @return
     */
    public PerformanceVO dataPerformance(DataPerformanceBO dataPerformanceBO) {
        PerformanceVO performanceVO = new PerformanceVO();
        //不传用户账号，则查询所有用户业绩
        if (ObjectUtils.isEmpty(dataPerformanceBO.getUserName())) {
            List<DataPerformanceVO> orders = purchaseRecordMapper.selectByUserIds(null);
            if (ObjectUtils.isEmpty(orders)) {
                throw new RuntimeException("未查询到订单");
            }
            performanceVO.setOrderCount((long) orders.size());
            performanceVO.setOrderList(orders);
            return performanceVO;
        }
        //入参传用户账号，则查询该用户下的业绩
        //根据传入用户名查询用户信息
        SysUser sysUser = userMapper.selectUserByUserName(dataPerformanceBO.getUserName());
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new RuntimeException(dataPerformanceBO.getUserName() + "用户不存在");
        }
        //根据用户id获取下级所有用户的id
        List<Long> userIds = inviteRecordService.selectTeamUserIds(sysUser.getUserId());
        if (ObjectUtils.isEmpty(userIds)) {
            throw new RuntimeException(dataPerformanceBO.getUserName() + "的团队暂无下级人员");
        }
        List<DataPerformanceVO> orders = purchaseRecordMapper.selectByUserIds(userIds);
        if (ObjectUtils.isEmpty(orders)) {
            throw new RuntimeException("未查询到订单");
        }
        performanceVO.setOrderCount((long) orders.size());
        performanceVO.setOrderList(orders);
        return performanceVO;
    }

    /**
     * 数据总览
     */
    public DataOverviewVO dataOverview() {
        DataOverviewVO dataOverviewVO = new DataOverviewVO();
        //查询所有用户数量
        dataOverviewVO.setMemberCount(userMapper.selectUserCount(null, null));
        //查询本周新增用户数量
        Date startDate = DateUtils.getWeekMonday();
        Date endDate = DateUtils.getWeekSunday();
        log.info("本周开始时间：{}，结束时间：{}", startDate, endDate);
        dataOverviewVO.setWeekMemberCount(userMapper.selectUserCount(startDate, endDate));
        //查询支出金额
        dataOverviewVO.setExpenditure(withdrawalRecordMapper.selectSumAmount());
        //查询盈利金额
        dataOverviewVO.setProfit(purchaseRecordMapper.selectSumAmount());
        return dataOverviewVO;
    }
}
